<template>
  <el-dialog
    :title="!dataForm.auditId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团长id" prop="leaderId">
      <el-input v-model="dataForm.leaderId" placeholder="团长id"></el-input>
    </el-form-item>
    <el-form-item label="所属城市" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="所属城市"></el-input>
    </el-form-item>
    <el-form-item label="自提点信息" prop="giveInformation">
      <el-input v-model="dataForm.giveInformation" placeholder="自提点信息"></el-input>
    </el-form-item>
    <el-form-item label="团长配送费" prop="leaderDeliveryFee">
      <el-input v-model="dataForm.leaderDeliveryFee" placeholder="团长配送费"></el-input>
    </el-form-item>
    <el-form-item label="审核人姓名" prop="auditName">
      <el-input v-model="dataForm.auditName" placeholder="审核人姓名"></el-input>
    </el-form-item>
    <el-form-item label="申请时间" prop="applyTime">
      <el-input v-model="dataForm.applyTime" placeholder="申请时间"></el-input>
    </el-form-item>
    <el-form-item label="审核时间" prop="auditTime">
      <el-input v-model="dataForm.auditTime" placeholder="审核时间"></el-input>
    </el-form-item>
    <el-form-item label="审核状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="审核状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          auditId: 0,
          leaderId: '',
          cityId: '',
          giveInformation: '',
          leaderDeliveryFee: '',
          auditName: '',
          applyTime: '',
          auditTime: '',
          state: ''
        },
        dataRule: {
          leaderId: [
            { required: true, message: '团长id不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '所属城市不能为空', trigger: 'blur' }
          ],
          giveInformation: [
            { required: true, message: '自提点信息不能为空', trigger: 'blur' }
          ],
          leaderDeliveryFee: [
            { required: true, message: '团长配送费不能为空', trigger: 'blur' }
          ],
          auditName: [
            { required: true, message: '审核人姓名不能为空', trigger: 'blur' }
          ],
          applyTime: [
            { required: true, message: '申请时间不能为空', trigger: 'blur' }
          ],
          auditTime: [
            { required: true, message: '审核时间不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '审核状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.auditId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.auditId) {
            this.$http({
              url: this.$http.adornUrl(`/leader/audit/info/${this.dataForm.auditId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.leaderId = data.audit.leaderId
                this.dataForm.cityId = data.audit.cityId
                this.dataForm.giveInformation = data.audit.giveInformation
                this.dataForm.leaderDeliveryFee = data.audit.leaderDeliveryFee
                this.dataForm.auditName = data.audit.auditName
                this.dataForm.applyTime = data.audit.applyTime
                this.dataForm.auditTime = data.audit.auditTime
                this.dataForm.state = data.audit.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/leader/audit/${!this.dataForm.auditId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'auditId': this.dataForm.auditId || undefined,
                'leaderId': this.dataForm.leaderId,
                'cityId': this.dataForm.cityId,
                'giveInformation': this.dataForm.giveInformation,
                'leaderDeliveryFee': this.dataForm.leaderDeliveryFee,
                'auditName': this.dataForm.auditName,
                'applyTime': this.dataForm.applyTime,
                'auditTime': this.dataForm.auditTime,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
